package cn.harry.modular.sys.service.impl;

import cn.harry.modular.sys.mapper.SysConfigMapper;
import cn.harry.modular.sys.domain.SysConfig;
import cn.harry.modular.sys.enums.CacheConstants;
import cn.harry.modular.sys.service.SysConfigService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统配置信息表/枚举值表
 *
 * @author honghh Date 2019-11-25 16:36:48 Copyright (C) www.tech-harry.cn
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig>
		implements SysConfigService {

	@Override
	public List<SysConfig> getExportList(SysConfig sysConfig) {
		LambdaQueryWrapper<SysConfig> wrapper = Wrappers.<SysConfig>lambdaQuery()
				.gt(StrUtil.isNotEmpty(sysConfig.getBeginTime()),
						SysConfig::getCreateTime, sysConfig.getBeginTime())
				.lt(StrUtil.isNotEmpty(sysConfig.getEndTime()), SysConfig::getCreateTime,
						sysConfig.getEndTime());
		return list(wrapper);
	}

	@Override
	@Cacheable(value = CacheConstants.PARAMS_DETAILS, key = "#paramKey", unless = "#result == null ")
	public String getKeyToValue(String paramKey) {
		SysConfig sysConfig = this.getOne(
				new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey, paramKey));
		if (sysConfig != null) {
			return sysConfig.getParamValue();
		}
		return null;
	}

}